package com.tjnu.losca.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tjnu.losca.mapper.GoodsMapper;
import com.tjnu.losca.pojo.Goods;
import com.tjnu.losca.service.IGoodsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author losca
 * @since 2022-02-13
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Override
    public IPage list(int pageNum, int pageSize, String search) {
        QueryWrapper<Goods> wrapper = new QueryWrapper<Goods>();
        if(StringUtils.isNotBlank(search)) {
            wrapper.like("name",search);
        }
        return this.page(new Page<Goods>(pageNum,pageSize),wrapper.orderByDesc("id"));
    }

    @Override
    public boolean del(String goodId) {
        //通过商品id获取商品信息
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("good_id", goodId);
        Goods good = this.getOne(wrapper);
        //0代表禁用 1代表可用
        good.setStatus(0);
        //修改商品状态
        return this.updateById(good);
    }

    @Override
    public boolean rec(String goodId) {
        //通过商品id获取商品信息
        QueryWrapper<Goods> wrapper = new QueryWrapper<>();
        wrapper.eq("good_id", goodId);
        Goods good = this.getOne(wrapper);
        //0代表禁用 1代表可用
        good.setStatus(1);
        //修改商品状态
        return this.updateById(good);
    }

    @Override
    public boolean mulDel(String[] ids) {
        //通过ids获取多个商品
        List<Goods> goods = new ArrayList<>();
        //遍历ids把用户状态设置为0
        for (String id : ids) {
            //通过商品id获取商品信息
            QueryWrapper<Goods> wrapper = new QueryWrapper<>();
            wrapper.eq("good_id", id);
            Goods good = this.getOne(wrapper);
            good.setStatus(0);
            goods.add(good);
        }
        //批量修改商品状态
        return this.updateBatchById(goods);
    }

    @Override
    public boolean update(Goods goods) {
       return this.updateById(goods);
    }
}
